package reduce.fz.com.reduce.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.MessageNotice;
import reduce.fz.com.reduce.bean.NotifyCention;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LeftMenuItem;
import reduce.fz.com.reduce.service.NoticeService;
import reduce.fz.com.reduce.util.ServiceBuilder;

/**
 * 消息中心activity
 */
public class MessageCenterActivity extends BaseToolBarActivity implements View.OnClickListener {

    private LeftMenuItem mFriend;
    private LeftMenuItem mPraise;
    private LeftMenuItem mComment;
    private NotifyCention notifyCention;
    private static final int FORWARD_FRIEND = 110;
    private static final int FORWARD_PRAISE = 111;
    private static final int FORWARD_COMMENT = 112;


    private NoticeService noticeService = ServiceBuilder.builderNoticeService(null);


    /**
     * 加载消息中心数据信息
     */
    public void loadNotifyCention() {
        noticeService.getNotifyCention(new AbResponseListener<NotifyCention>(new TypeReference<Result<NotifyCention>>() {
        }) {
            @Override
            public void error(ServiceException e) {
            }
            @Override
            public void responseInfo(Result<NotifyCention> data) {
                notifyCention = data.getData();
                //更新小红点状态
                updateStatus();
            }
        });
    }

    /**
     * 更新
     */
    public void updateStatus() {
        mFriend.setPoint(notifyCention.ADD_FOLLOW);
        mComment.setPoint(notifyCention.REPLY_COMMENT);
        mPraise.setPoint(notifyCention.PRAISE);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarTitle("我的消息");
        setContentView(R.layout.activity_message_center);
        initWidget();
        EventBus.getDefault().register(this);
        loadNotifyCention();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initWidget() {

        mFriend = (LeftMenuItem) findViewById(R.id.menu_friend);
        mPraise = (LeftMenuItem) findViewById(R.id.menu_praise);
        mComment = (LeftMenuItem) findViewById(R.id.menu_comment);
        mFriend.setOnClickListener(this);
        mPraise.setOnClickListener(this);
        mComment.setOnClickListener(this);

    }

    @Override
    public void initListener() {

    }

    public void onEvent(MessageNotice notice) {
        if (notice.type.equals(MessageNotice.TYPE_NOTICE_COMMENT))
            mFriend.setPointAdd(1);
        else if (notice.type.equals(MessageNotice.TYPE_NOTICE_FOLLOW)) {
            mComment.setPointAdd(1);
        } else if (notice.type == MessageNotice.TYPE_NOTICE_PRAISE)
            mPraise.setPoint(1);
    }


    /**
     * 跳转到朋友页面
     */
    public void forwardFriend() {
        Intent intent = new Intent(this, MessageFriendActivity.class);
        startActivityForResult(intent, FORWARD_FRIEND);
    }

    /**
     * 跳转到赞
     */
    public void forwardPraise() {
        Intent intent = new Intent(this, MessagePraiseActivity.class);
        startActivityForResult(intent, FORWARD_PRAISE);

    }

    /**
     * 跳转到回复我的消息
     */
    public void forwardComment() {
        Intent intent = new Intent(this, MessageCommentActivity.class);
        startActivityForResult(intent, FORWARD_COMMENT);
    }

    public void clearNoReadMessage(String type) {
        noticeService.clearNoReadMessage(type, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
        }) {
            @Override
            public void error(ServiceException e) {
            }

            @Override
            public void responseInfo(Result<HashMap> data) {
            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case FORWARD_COMMENT:
                mComment.setPoint(0);
                break;
            case FORWARD_FRIEND:
                mComment.setPoint(0);
                break;
            case FORWARD_PRAISE:
                mComment.setPoint(0);
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.menu_friend:
                forwardFriend();
                break;
            case R.id.menu_praise:
                forwardPraise();
                break;
            case R.id.menu_comment:
                forwardComment();
                break;
            default:
                break;
        }

    }
}
